package com.smartthinking.CICPNL.home.activity;

import java.util.ArrayList;
import java.util.Dictionary;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.ViewGroup.MarginLayoutParams;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;

import com.smartthinking.CICPNL.R;
import com.smartthinking.CICPNL.R.id;
import com.smartthinking.CICPNL.home.objects.App_ImageLoader;
import com.smartthinking.CICPNL.home.objects.App_ImageLoader_Scale;
import com.smartthinking.CICPNL.home.objects.clsDictionary;
import com.smartthinking.CICPNL.home.objects.clsImagesShow;
import com.smartthinking.CICPNL.home.xml.saxXML;

public class MensajesDetalle extends Activity   {

	private Dictionary dic;
	public ArrayList<Dictionary> Actividades;
	public static ArrayList<Dictionary<String,String>> TraeDatos = null;
	public Gallery gallery1;
	public Hoteles_Adapter myAdapter;
	public App_ImageLoader_Scale imageLoader;
	private Intent intent;
	public App_ImageLoader imageLoader2;
	public int EVEIDR;
	
	public TextView txtNomMsg;
	

	public void voidFinish(View V) {
		finish();

	}
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);

		// Checks the orientation of the screen
		 
	}
	public void abrirMenuLateral(View V) {
		intent = new Intent(MensajesDetalle.this, MenuLateral.class);

		startActivity(intent);
	}

	@Override
	public void onBackPressed() {
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.ui_mensajes_detalle);

		EVEIDR = Utilities.EVE_IDR;
		
		CargarContenido();
	
	}
    
  
	private void CargarContenido() {
		// TODO Auto-generated method stub

		new AsyncContenido().execute("");

	}
	
	private class AsyncContenido extends AsyncTask<String, String, String>{
		
		@Override
		protected void onPreExecute() {
			super.onPreExecute();
			
			//progressDialog = ProgressDialog.show(Programa.this, "","Cargando...");

		}

		@Override
		protected String doInBackground(String... params) {
			Intent intent = getIntent();
		    String idmsg = intent.getStringExtra("idmsg");
		    
		    String qry ="select * from push where eve_idr=" + EVEIDR + " and push_idr=" + idmsg;
		    
			TraeDatos = Utilities.qry2(getApplicationContext(),qry);
			return null;
			
		}
		
		
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			
			txtNomMsg = (TextView) findViewById(id.tv_msgdetalle_contenido);
			txtNomMsg.setText(TraeDatos.get(0).get("push_text").toString());
		}
		
	}

	
	
	public void abrirWeb( ) {
		Intent intent = new Intent(MensajesDetalle.this, Web.class);
		intent.putExtra("PUTBOOL", true);
		intent.putExtra("PUTWEB", dic.get("not_descripcion").toString());
		startActivity(intent);
	}

	private void cargarNoticia() {
		new AsyncActividades().execute("contenido");

	}

	private class AsyncActividades extends AsyncTask<String, Void, Integer> {

		protected void onPostExecute(Integer in) {

			gallery1 = (Gallery) findViewById(R.id.galery_fotos);

			myAdapter = new Hoteles_Adapter(Actividades);

			gallery1.setAdapter(myAdapter);

			DisplayMetrics metrics = new DisplayMetrics();
			MensajesDetalle.this.getWindowManager().getDefaultDisplay()
					.getMetrics(metrics);

			// Gallery gallery = (Gallery) findViewById(R.id.gallery1);

			MarginLayoutParams mlp = (MarginLayoutParams) gallery1
					.getLayoutParams();
			mlp.setMargins(-(metrics.widthPixels / 2 + 50), mlp.topMargin,
					mlp.rightMargin, mlp.bottomMargin);

			
			gallery1.setOnItemClickListener(new OnItemClickListener() {
				public void onItemClick(AdapterView<?> parent, View view,
						int position, long id) {

					// Toast.makeText(
					// Calendario.this,
					// gallery1_.get(position).get("eve_nombre")
					// .toString(), Toast.LENGTH_LONG).show();

					// abrirInformacionEvento( Actividades);

					Intent intent = new Intent(MensajesDetalle.this,
							Imagenes.class);

					clsImagesShow.setDctionary(Actividades);

					startActivity(intent);

				}
			});

		}

		protected void abrirInformacionEvento(Dictionary d) {

			// TODO Auto-generated method stub

		}

		protected void onPreExecute() {

		}

		protected Integer doInBackground(String... urls) {

			String qry_url = getString(R.string.QRY_URL)
					+ Uri.encode("select * from noticias_fotos where not_idr="
							+ dic.get("not_idr").toString());

			saxXML saxparser = new saxXML(qry_url);

			Actividades = saxparser.parse();

			return null;

		}

	}

	public class Hoteles_Adapter extends BaseAdapter {

		private LayoutInflater inflater = null;
		private ArrayList<Dictionary> data;

		public Hoteles_Adapter(ArrayList<Dictionary> d) {

			data = d;
			inflater = (LayoutInflater) MensajesDetalle.this
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			//
			imageLoader2 = new App_ImageLoader(
					MensajesDetalle.this.getApplicationContext());

		}

		public int getCount() {
			return data.size();
		}

		public Object getItem(int position) {
			return position;
		}

		public long getItemId(int position) {
			return position;
		}

		public class ViewHolder {

			public ImageView icon;

		}

		public View getView(int position, View convertView, ViewGroup parent) {

			View vi = convertView;
			ViewHolder holder;

			if (convertView == null) {

				vi = inflater.inflate(R.layout.ui_fotos_items, null);

				holder = new ViewHolder();

				holder.icon = (ImageView) vi
						.findViewById(R.id.iv_foto_item_image);

				vi.setTag(holder);

			} else

				holder = (ViewHolder) vi.getTag();

			//
			String url_img = getString(R.string.QRY_URL_IMG)
					+ data.get(position).get("foto_url").toString()
							.replace(" ", "%20");

			holder.icon.setTag(url_img);
			try {

				imageLoader.DisplayImage(url_img, MensajesDetalle.this,
						holder.icon);

			} catch (Exception ex) {

				String s = ex.getMessage().toString();

			}

			return vi;

		}

	}

 

}
